/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.util;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.mol.market.R;

/**
 * 用于定制各种不同类型的通知
 * @author Chaos	
 *
 */
public class NotificationUtils {
    

    private static NotificationManager newInstance(Context c) {
        return (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
    }
    
    /**
     * 创建一个通用的通知(暂不对外开放)
     * @param context   
     * @param ticker    滚动的文字
     * @param icon      未拉开抽屉时的icon
     * @param target    跳转的目标类
     * @param title     拉开抽屉时的title
     * @param content   拉开抽屉时的content
     * @param contentIcon 拉开抽屉时的icon
     */
    private static void createCommonNotification(Context context, CharSequence ticker, int icon,
            Class<? extends Activity> target, CharSequence title, CharSequence content,
            int contentIcon) {
            Intent intent = new Intent(context,target);
            createCommonNotification(
                    context,
                    ticker,
                    icon,
                    intent,
                    title,
                    content,
                    contentIcon);
    }
    
    /**
     * 创建一个通用的通知(暂不对外开放)
     * @param context   
     * @param ticker    滚动的文字
     * @param icon      未拉开抽屉时的icon
     * @param intent    跳转的所需的intent
     * @param title     拉开抽屉时的title
     * @param content   拉开抽屉时的content
     * @param contentIcon 拉开抽屉时的icon
     */
    private static void createCommonNotification(Context context, CharSequence ticker, int icon,
            Intent intent, CharSequence title, CharSequence content,
            int contentIcon) {
            Notification notification = new Notification();
            notification.defaults = Notification.DEFAULT_SOUND;
            notification.icon = icon;
            notification.tickerText = ticker;
            notification.when = System.currentTimeMillis();
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            notification.setLatestEventInfo(context, title, content, pendingIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            newInstance(context).notify(contentIcon, notification);
    }
    
    /**
     * 创建一个通用的通知,并使用默认的icon
     * @param context
     * @param ticker
     * @param target
     * @param title
     * @param content
     */
    public static void createCommonWrapNotification(Context context, CharSequence ticker,
            Class<? extends Activity> target, CharSequence title, CharSequence content) {
        createCommonNotification(
                context, 
                ticker, 
                R.drawable.notification_icon, 
                target, 
                title, 
                content,
                R.drawable.notification_icon);
    }
}
